//
//  ViewController.swift
//  swift 瀑布流
//
//  Created by adel on 2017/9/18.
//  Copyright © 2017年 adel. All rights reserved.
//

import UIKit
let  cellID = "cellID"

class ViewController: UIViewController {

    fileprivate lazy var collectionView : UICollectionView = {
        
        let layout = WaterfallLayout()
        layout.dataSource = self
        layout.minimumLineSpacing = 5
        layout.minimumInteritemSpacing  = 5
        layout.sectionInset.left = 5
        layout.sectionInset.right = 15
        layout.sectionInset.top = 10
        
        let collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
        return collectionView
        
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupCollectionView()
    }

}


extension ViewController {
    
  fileprivate func setupCollectionView() {
    view.addSubview(collectionView)
    collectionView.dataSource = self
    collectionView.delegate = self
    collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellID)
        
    }
}

extension ViewController : WaterfallLayoutDataSource{
    
    func waterfallLayout(_ layout: WaterfallLayout, indexPath: IndexPath) -> CGFloat {
        
        return CGFloat(arc4random_uniform(200) + 50);
    }
    
    func numberOfColsInWaterfallLayout(_ layout: WaterfallLayout) -> Int {
        
        return 4;
    }
    
}
extension ViewController : UICollectionViewDelegate, UICollectionViewDataSource{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return 80;
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let  cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath)
        cell.backgroundColor = UIColor.randomColor()
        return cell
        
    }
    
    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
        
    }
    
}
